﻿using System;
using System.Windows.Forms;

namespace BTVPodcatcherNamespace
{
    public partial class DownloadViewer : Form
    {
        public DownloadViewer()
        {
            InitializeComponent();
        }
        public void setupForm()
        {
            dowloadInfoBindingSource.DataSource = ((MainForm)this.MdiParent).allDownloadingItems;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {   try
            {
                int i =dataGridView1.FirstDisplayedScrollingRowIndex;
                DataGridViewSelectedRowCollection selected = dataGridView1.SelectedRows;
                dowloadInfoBindingSource.ResetBindings(false);
                if (i<dataGridView1.RowCount&&i>=0)
                {
                    dataGridView1.FirstDisplayedScrollingRowIndex = i;
                }
            
                foreach (DataGridViewRow dgv in selected)
                {
                    dgv.Selected = true;
                }
            }
            catch (Exception)
            {

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (dowloadInfoBindingSource.Current != null)
                {
                    if (((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus == DownloadStatus.Downloading || ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus == DownloadStatus.Ready)
                        ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus = DownloadStatus.CancelRequested;
                }
            }
            catch (Exception)
            {
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {

                if (dowloadInfoBindingSource.Current != null)
                {
                    ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus = DownloadStatus.CancelRequested;
                    ((MainForm)this.MdiParent).allDownloadingItems.Remove(((DowloadInfo)dowloadInfoBindingSource.Current));
                }
                ((MainForm)this.MdiParent).downloadRemoved();
            }
            catch (Exception)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DowloadInfo di in dowloadInfoBindingSource)
                {
                    if (di.DownloadStatus == DownloadStatus.Downloading || di.DownloadStatus == DownloadStatus.Ready)
                        di.DownloadStatus = DownloadStatus.CancelRequested;
                }
            }
            catch (Exception)
            {
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                while (((MainForm)this.MdiParent).allDownloadingItems.Count > 0)
                {

                    ((MainForm)this.MdiParent).allDownloadingItems[0].DownloadStatus = DownloadStatus.CancelRequested;
                    ((MainForm)this.MdiParent).allDownloadingItems.Remove(((MainForm)this.MdiParent).allDownloadingItems[0]);
                    
                }
                ((MainForm)this.MdiParent).downloadRemoved();
            }
            catch (Exception e2)
            {
                ConsoleForm.addMessage("Error " + e2.ToString());
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            System.Console.WriteLine(e.Exception.ToString());
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null)
            {
                if (dataGridViewTextBoxColumn3.DisplayIndex == e.ColumnIndex && e.Value != null)
                {
                    e.Value = string.Format("{0:#,###,###} kBps", (((long)e.Value) / 1024.0));
                    e.FormattingApplied = true;
                }
                else if (dataGridViewTextBoxColumn5.DisplayIndex == e.ColumnIndex)
                {
                    if (e.Value.Equals(DownloadStatus.CancelRequested))
                    {
                        e.Value = "Cancel Requested";
                        e.FormattingApplied = true;
                    }
                    else if (e.Value.Equals(DownloadStatus.PropertiesWritten))
                    {
                        e.Value = "BTV Properties Written";
                        e.FormattingApplied = true;
                    }
                    else if (e.Value.Equals(DownloadStatus.WaitingToWriteProperties))
                    {
                        e.Value = "Waiting to Write BTV Properties";
                        e.FormattingApplied = true;
                    }
                    else if (e.Value.Equals(DownloadStatus.FailedToWriteProperties))
                    {
                        e.Value = "Failed to Write BTV Properties";
                        e.FormattingApplied = true;
                    }
                }
            }
            
        }

        private void writeBTVMediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dowloadInfoBindingSource.Current != null)
            {
                DowloadInfo di = ((DowloadInfo)dowloadInfoBindingSource.Current);
                di.DownloadStatus = DownloadStatus.WaitingToWriteProperties;
                BTVWriter btw = new BTVWriter(di, (MainForm)this.MdiParent);

                System.Threading.Thread btvWriter = new System.Threading.Thread(new System.Threading.ThreadStart(btw.writeBTVProprties));
                btvWriter.Name = "BTV Podcatcher Media Tag Writer";
                btvWriter.IsBackground = true;
                btvWriter.Start();
            }
        }

        private void contextMenuStrip1_Opened(object sender, EventArgs e)
        {
            writeBTVMediaToolStripMenuItem.Visible = false;
            if (dowloadInfoBindingSource.Current != null)
            {
                DowloadInfo di = ((DowloadInfo)dowloadInfoBindingSource.Current);
                if (di.DownloadStatus == DownloadStatus.Complete || di.DownloadStatus == DownloadStatus.PropertiesWritten || di.DownloadStatus == DownloadStatus.Error || di.DownloadStatus == DownloadStatus.FailedToWriteProperties)
                {
                    writeBTVMediaToolStripMenuItem.Visible = true;
                }
            }
        }
    }
}
